﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication4
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = username.Text;
            string pwd = password.Text;

            string connStr = "server=.;uid=sa;pwd=123456;database=Student_db";
            //创建SQLConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库连接
                conn.Open();
                string sql = "select * from StudentInfo where stu_name=@name and password=@pwd";
                //填充SQL语句
                SqlParameter[] pars =
                {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",pwd)
                 };
                //创建SqlCommand对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行SQL语句
                int returnvalue = cmd.ExecuteNonQuery();
                //判断SQL语句是否执行成功
                if (returnvalue != 0)
                {
                    Literal1.Text ="登录成功！";
                }
                else
                {
                    Literal1.Text = "登录失败！";
                }
            }
            catch (Exception ex)
            {
                Literal1.Text = "注册失败！" + ex.Message;
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }
    }
}